package BookManagement.dao;

import BookManagement.domain.Comment;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public interface CommentDao {

    @Insert("insert into comment values(#{userName},#{bookISBN},#{bookName},#{author},#{content},#{time})")
    public int addComment(Comment comment);

    @Select("<script>"+"select * from comment where bookISBN=#{bookISBN}"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>")
    public List<Comment> findCommentsByBook(HashMap<String,Object> map);

    @Select("<script>"+"select * from comment where username=#{username}"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>")
    public List<Comment> findCommentsByUser(HashMap<String,Object> map);

    @Delete("delete from comment where userName=#{userName} and bookISBN=#{bookISBN}")
    public int deleteComment(HashMap<String,Object> map);

    @Select("select count(*) from comment where bookISBN=#{bookISBN}")
    public int findCommentCountForBook(String bookISBN);

    @Select("select count(*) from comment where username=#{userName}")
    public int findCommentCountForUser(String userName);

    @Select("<script>"+"select * from comment where username=#{username} and bookISBN=#{bookISBN}"
            +"</script>")
    public Comment findOnlyComment(HashMap<String,Object> map);
}
